package com.eece496.Server;

import java.io.IOException;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eece496.Database.Assignments;
import com.eece496.Database.COGS;
import com.eece496.Database.COGSAssignments;
import com.eece496.Database.Enrollment;
import com.eece496.Database.Grades;
import com.eece496.Helpers.StudentInfo;

/**
 * Servlet implementation class ProfAddCOGSServlet
 */
public class ProfAddCOGSServlet extends MasterServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProfAddCOGSServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Connect();
		String courseNumber = request.getParameter("courseNumber");
		String sectionNumber = request.getParameter("sectionNumber");
		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");
		
		//System.out.println("Course Number:" + courseNumber);
		String COGSName = request.getParameter("CogsName");
		
		String COGSNum = COGSName.substring(4);
		System.out.println(COGSNum);
		
		//String start
		
		String divisionOne = request.getParameter("division0");
		String divisionTwo = request.getParameter("division1");
		
		String startTimeOne= request.getParameter("Starttime0");
		String endTimeOne = request.getParameter("Endtime0");
		
		String startTimeTwo= request.getParameter("Starttime1");
		String endTimeTwo = request.getParameter("Endtime1");
				
		
		
		COGS COGSDB1 = new COGS(COGSName, divisionOne, startTimeOne, endTimeOne, courseNumber, sectionNumber);
		COGS COGSDB2 = new COGS(COGSName, divisionTwo, startTimeTwo, endTimeTwo, courseNumber, sectionNumber);
		COGSDB1.Setup(connection);
		COGSDB2.Setup(connection);
		Enrollment EnrollmentDB = new Enrollment();
		EnrollmentDB.Setup(connection);
		try {
			
			//Add a Session
			COGSDB1.Insert();
			COGSDB2.Insert();
			
			//Add Assignments
			COGSAssignments iAssignmentGroup = new COGSAssignments(1, "Ind"+COGSNum, 10, COGSName, courseNumber, sectionNumber);
			iAssignmentGroup.Setup(connection);
			iAssignmentGroup.Insert();
			
			//Add the Individual Grades to each enrolled student
			ArrayList<StudentInfo> list = new ArrayList<StudentInfo>();
			list = EnrollmentDB.GetAllStudentsEnrolled(courseNumber, sectionNumber);
			
			//System.out.println(list.size());
			for(StudentInfo info: list)
			{
				Grades GradesDB = new Grades("Ind"+COGSNum, courseNumber, sectionNumber, info.studentID, "NA", "individual");
				GradesDB.Setup(connection);
				GradesDB.Insert();
			}
			
			
			for(int i=0; i <6; i++)
			{
				
				COGSAssignments cAssignmentGroup = new COGSAssignments((i+2), COGSName+"Group"+(i+1), 10, COGSName, courseNumber, sectionNumber);
				cAssignmentGroup.Setup(connection);
				cAssignmentGroup.Insert();
				for(StudentInfo info: list)
				{
					Grades GradesDB = new Grades(COGSName+"Group"+(i+1), courseNumber, sectionNumber, info.studentID, "NA", "group");
					GradesDB.Setup(connection);
					GradesDB.Insert();
				}
			}
					
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		request.setAttribute("courseNumber", courseNumber);
		request.setAttribute("sectionNumber", sectionNumber);
		request.setAttribute("firstname", firstname);
		request.setAttribute("lastname", lastname);
		request.getRequestDispatcher("/Professor/Professor-Course-Portal.jsp")
		.forward(request, response);
	}


}
